package com.example.wechattakeaway.service.Impl;

import com.example.wechattakeaway.entity.OptionDetail;
import com.example.wechattakeaway.enums.ResultEnum;
import com.example.wechattakeaway.form.meroptionsform.InsertOptionDetailForm;
import com.example.wechattakeaway.form.meroptionsform.UpdateOptionDetailForm;
import com.example.wechattakeaway.mapper.OptionDetailMapper;
import com.example.wechattakeaway.service.OptionDetailService;
import com.example.wechattakeaway.utils.PageUtil;
import com.example.wechattakeaway.utils.ResultVOUtil;
import com.example.wechattakeaway.vo.ResultVO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Repository
public class OptionDetailServiceImpl implements OptionDetailService {
    @Autowired
    OptionDetailMapper optionDetailMapper;

    //插入详情
    @Override
    public ResultVO insertOptionDetail(InsertOptionDetailForm insertOptionDetailForm) {
        OptionDetail optionDetail=new OptionDetail();
        optionDetail.setOptionDetail(insertOptionDetailForm.getOptionDetail());
        optionDetail.setOptionId(insertOptionDetailForm.getOptionId());
        try {

            optionDetailMapper.insert(optionDetail);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success();
    }
    //修改详情
    @Override
    public ResultVO updateOptionDetail(UpdateOptionDetailForm updateOptionDetailForm) {
        OptionDetail optionDetail=new OptionDetail();
        optionDetail.setOptionDetail(updateOptionDetailForm.getOptionDetail());
        optionDetail.setOptionDetailId(updateOptionDetailForm.getOptionDetailId());
        try {

            optionDetailMapper.updateByPrimaryKeySelective(optionDetail);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success();
    }

    //根据id查询详情
    @Override
    public ResultVO selectDetailById(Integer optionDetailId) {
        OptionDetail optionDetail=new OptionDetail();
        try {

            optionDetail=optionDetailMapper.selectByPrimaryKey(optionDetailId);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success(optionDetail);
    }

    @Override
    public ResultVO selectDetailByOptionId(Integer pageNum,Integer optionId) {
        PageHelper.startPage(pageNum,5);

        List<OptionDetail> list=new ArrayList<>();
        try {

            list= optionDetailMapper.selectByOptionId(optionId);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap = PageUtil.getMap(list);

        return ResultVOUtil.success(hashMap);
    }

    @Override
    public ResultVO deleteOptionDetail(Integer optionDetailId) {
        try {

            optionDetailMapper.deleteByPrimaryKey(optionDetailId);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success();
    }

    @Override
    public ResultVO selectByOptionId(Integer optionId) {
       // PageHelper.startPage(pageNum,5);

        List<OptionDetail> list=new ArrayList<>();
        try {

            list= optionDetailMapper.selectByOptionId(optionId);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        HashMap<String,Object> hashMap = new HashMap<>();
       // hashMap = PageUtil.getMap(list);
        hashMap.put("list",list);

        return ResultVOUtil.success(hashMap);
    }
}
